<template>
  <VButton :auth="auth" @click="open()">批量开启拉单</VButton>
  <el-dialog v-if="load" title="批量开启拉单" v-model="visible" append-to-body :close-on-click-modal="false" :z-index="2000" width="500px" @close="handleClose">
    <el-form label-position="top">
      <el-form-item label="是否下载订单/报表">
        <el-radio-group v-model="form.is_download_orders">
          <el-radio v-for="(d, i) in options.edit_pull_order" :key="i" :label="d.value">{{d.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否预生成追踪号">
        <el-radio-group v-model="form.is_pre_generate">
          <el-radio v-for="(d, i) in options.edit_pull_order" :key="i" :label="d.value">{{d.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否标记平台发货">
        <el-radio-group v-model="form.is_platform_shipment">
          <el-radio v-for="(d, i) in options.edit_pull_order" :key="i" :label="d.value">{{d.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
  tableRef: { type: Object, default: () => ({}) },
  auth: { type: String, default: '' },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})

// 打开弹窗
const open = async () => {
  const selection = props.tableRef.$table.getCheckboxRecords()
  if (!selection?.length) {
    ElMessage.warning('请先选择行!')
    return
  }
  const shop_ids = selection.map(d => d.shop_id)
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = {shop_ids, is_download_orders: 1, is_pre_generate: 1, is_platform_shipment: 1}
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  loadings.submit = true
  api.editOrder(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>